package com.hsmw.schedule.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyun.dyvmsapi20170525.models.SingleCallByVoiceResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsmw.schedule.mapper.HsmwDriverUserMapper;
import com.hsmw.schedule.mapper.HsmwMonitorDriverMessageMapper;
import com.hsmw.schedule.mapper.HsmwUserDriverMapper;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.HsmwDriverUser;
import com.hsmw.domain.HsmwMonitorDriverMessage;
import com.hsmw.domain.HsmwUserDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@EnableScheduling
@Configuration
@Slf4j
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class Monitor {


    @Autowired
    private HsmwMonitorDriverMessageMapper hsmwMonitorDriverMessageMapper;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;

    @Autowired
    private HsmwDriverUserMapper hsmwDriverUserMapper;

    final static String voiceCode = "0e7fe15a-5f6a-412f-8172-9f324d8c3bce.wav";


    @Scheduled(cron = "0 */5 * * * ?")
    private void monitorDriver(){
        log.info("start：" + new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        List<HsmwMonitorDriverMessage> hsmwMonitorDriverMessages = hsmwMonitorDriverMessageMapper.selectList(new LambdaQueryWrapper<HsmwMonitorDriverMessage>().eq(HsmwMonitorDriverMessage::getStatus, "0"));
        if(CollectionUtil.isNotEmpty(hsmwMonitorDriverMessages)){
            for (HsmwMonitorDriverMessage hsmwMonitorDriverMessage : hsmwMonitorDriverMessages) {
                Long queryTime = System.currentTimeMillis();
                try {

                    String result = Sample.QueryCallDetailByCallId(Sample.createDyvmsapiClient(), hsmwMonitorDriverMessage.getCallId(), queryTime);
                    JSONObject jsonObject = JSONUtil.parseObj(result);
                    String data = jsonObject.getStr("data");
                    if(data != null){
                        JSONObject json = JSONUtil.parseObj(data);
                        Integer duration = json.getInt("duration");
                        if(duration == 0){
                            // 通话时间为0 证明没有接通
                            List<HsmwUserDriver> userDrivers = hsmwUserDriverMapper.selectList(new LambdaQueryWrapper<HsmwUserDriver>().eq(HsmwUserDriver::getVehicleId, hsmwMonitorDriverMessage.getVehicleId()));
                            for (HsmwUserDriver userDriver : userDrivers) {
                                HsmwDriverUser hsmwDriverUser = hsmwDriverUserMapper.selectById(userDriver.getUserId());
                                if(StringUtils.equals(hsmwDriverUser.getUsername(),hsmwMonitorDriverMessage.getDirverPhone())){
                                    continue;
                                }
                                SingleCallByVoiceResponse singleCallByVoiceResponse = Sample.SingleCallByVoice(Sample.createDyvmsapiClient(), hsmwDriverUser.getUsername(), voiceCode);
                                hsmwMonitorDriverMessage.setStatus("1");
                                hsmwMonitorDriverMessageMapper.updateById(hsmwMonitorDriverMessage);
                            }
                        }else {
                            hsmwMonitorDriverMessage.setStatus("1");
                            hsmwMonitorDriverMessageMapper.updateById(hsmwMonitorDriverMessage);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        log.info("end：" + new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

    }


}
